﻿using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using Wix.Win8SetupBootstrapper.Classes;
using Wix.Win8SetupBootstrapper.Properties;

namespace Wix.Win8SetupBootstrapper.ViewModels
{
    public class WelcomePageViewModel : PageViewModelBase
    {
        private FrameworkElement _licenseDocument;
        private bool _isLicenseApplied;
        private string _installFolder;

        public override bool CanBack { get { return false; } }

        public override bool CanCancel
        {
            get { return true; }
        }


        public FrameworkElement LicenseDocument
        {
            get { return _licenseDocument; }
            set { _licenseDocument = value; OnPropertyChanged(); }
        }

        public bool IsLicenseApplied
        {
            get { return _isLicenseApplied; }
            set
            {
                _isLicenseApplied = value;
                OnPropertyChanged();
                CanNext = value;
            }
        }

        public string InstallFolder
        {
            get
            {
                if (string.IsNullOrEmpty(_installFolder))
                {
                    _installFolder = ReadVariableFromBundle(this.RootViewModel.Configuration.Ui.Pages.Welcome.InstallDirVariable);

                    if (!string.IsNullOrEmpty(_installFolder))
                    {
                        _installFolder = RootViewModel.Bootstrapper.Engine.FormatString(_installFolder);
                        _installFolder = _installFolder.Trim('"');

                        WriteVariableToBundle(this.RootViewModel.Configuration.Ui.Pages.Welcome.InstallDirVariable, _installFolder);
                    }
                }
                return _installFolder;
            }
            set
            {
                _installFolder = value;
                WriteVariableToBundle(this.RootViewModel.Configuration.Ui.Pages.Welcome.InstallDirVariable, value);

                OnPropertyChanged();
            }
        }

        public ICommand BrowseDirectoryCommand
        {
            get
            {
                return new BaseCommand(delegate
                {
                    var folderBrowserDialog = new FolderBrowserDialog { SelectedPath = InstallFolder };

                    folderBrowserDialog.ShowDialog();
                    InstallFolder = folderBrowserDialog.SelectedPath;

                }, delegate { return true; });

            }
        }


        public WelcomePageViewModel(RootViewModel rootViewModel)
            : base(rootViewModel)
        {
            string licenseFile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "license.rtf");
            string text = string.Empty;
            if (File.Exists(licenseFile))
            {
                text = File.ReadAllText(licenseFile);
            }
            else
            {
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Wix.Win8SetupBootstrapper.License.rtf"))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        text = reader.ReadToEnd();
                    }
                }
            }

            using (var stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(text)))
            {
                FlowDocument flowDocument = new FlowDocument();
                TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
                textRange.Load(stream, System.Windows.DataFormats.Rtf);

                this.LicenseDocument = new System.Windows.Controls.RichTextBox(flowDocument)
                {
                    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                    IsReadOnly = true,
                };
            }
        }

    }
}